<template>
  <div class="app-container">

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="item in langList" :key="item.id">
        <span slot="label">{{ item.langCn }}</span>
        <el-button
          type="text"
          icon="el-icon-edit"
          size="mini"
          slot="label"
          @click="handleUpdate(item)"
        ></el-button>
      </el-tab-pane>
      <el-tab-pane name="xz">
        <el-button
          type="text"
          plain
          icon="el-icon-plus"
          size="mini"
          slot="label"
          @click="handleAdd"
        >新增</el-button>
      </el-tab-pane>
      <translate :someValue="propId+''" :langCn="langCn"></translate>
    </el-tabs>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['video:lang:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--&lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--&lt;!&ndash;        <el-button&ndash;&gt;-->
<!--&lt;!&ndash;          type="warning"&ndash;&gt;-->
<!--&lt;!&ndash;          plain&ndash;&gt;-->
<!--&lt;!&ndash;          icon="el-icon-download"&ndash;&gt;-->
<!--&lt;!&ndash;          size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;          @click="handleExport"&ndash;&gt;-->
<!--&lt;!&ndash;          v-hasPermi="['video:lang:export']"&ndash;&gt;-->
<!--&lt;!&ndash;        >导出</el-button>&ndash;&gt;-->
<!--&lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--    </el-row>-->

<!--    <el-table v-loading="loading" :data="langList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
<!--      <el-table-column label="语言标识" align="center" prop="lang" />-->
<!--      <el-table-column label="中文简称" align="center" prop="langCn" />-->
<!--      <el-table-column label="电话国际区号" align="center" prop="nationCode" />-->
<!--      <el-table-column label="货币标准符号" align="center" prop="currency" />-->
<!--      <el-table-column label="积分兑换比例" align="center" prop="exchangeRate" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->

<!--    &lt;!&ndash; 添加或修改语言包对话框 &ndash;&gt;-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-row>
        <el-col style="background-color: #2795ff;color: #FFFFFF;padding: 10px;border-radius: 5px;line-height: 20px">
          <div>积分兑换比例</div>
          <div>1CNY = 10000</div>
          <div>1USD = 7.1CNY = 71000</div>
          <div>1HKD = 0.91CNY = 9100</div>
        </el-col>
        <el-col>
          <span class="form-item-desc">请前往百度翻译Api文档中查看《常见语种列表》。（如您是已认证的尊享版用户可联系管理员增加您需要的翻译语种类型）<a href="https://fanyi-api.baidu.com/doc/21" target="_blank" style="color: #2795ff">点击前往百度翻译Api</a></span>
        </el-col>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="语言ID" prop="id">
          <el-input v-model="form.id" :disabled="true" />
        </el-form-item>
        <el-form-item label="语言标识" prop="lang">
          <el-input v-model="form.lang" :disabled="disabled" placeholder="请输入语言标识" />
        </el-form-item>
        <el-form-item label="中文简称" prop="langCn">
          <el-input v-model="form.langCn" placeholder="请输入中文简称" />
        </el-form-item>
        <el-form-item label="电话国际区号" prop="nationCode">
          <el-input v-model="form.nationCode" placeholder="请输入电话国际区号" />
        </el-form-item>
        <el-form-item label="货币标准符号" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入货币标准符号" />
        </el-form-item>
        <el-form-item label="积分兑换比例" prop="exchangeRate">
          <el-input v-model="form.exchangeRate" placeholder="请输入积分兑换比例" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLang, getLang, delLang, addLang, updateLang, getListLang } from "@/api/video/lang";
import translate from "@/views/video/translate";

export default {
  name: "Lang",
  components: {
    translate
  },
  data() {
    return {
      // 标签页
      activeName: '0',
      propId:0,
      langCn: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 语言包表格数据
      langList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        lang: null,
        langCn: null,
        nationCode: null,
        currency: null,
        exchangeRate: null
      },
      // 表单参数
      form: {
        lang: ''
      },
      // 表单校验
      rules: {
        lang: [
          { required: true, message: "语言标识不能为空", trigger: "blur" }
        ],
        langCn: [
          { required: true, message: "中文简称不能为空", trigger: "blur" }
        ],
        nationCode: [
          { required: true, message: "电话国际区号不能为空", trigger: "blur" }
        ],
        currency: [
          { required: true, message: "货币标准符号不能为空", trigger: "blur" }
        ],
        exchangeRate: [
          { required: true, message: "积分兑换比例不能为空", trigger: "blur" }
        ]
      },
      disabled: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 切换页签触发事件 */
    handleClick() {
      if (this.langList[this.activeName]) {
        this.propId = this.langList[this.activeName].id + '';
        this.langCn = this.langList[this.activeName].langCn;
      }
    },
    /** 查询语言包列表 */
    getList() {
      this.loading = true;
      getListLang(this.queryParams).then(response => {
        this.langList = response.rows;
        this.total = response.total;
        this.handleClick();
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lang: null,
        langCn: null,
        nationCode: null,
        currency: null,
        exchangeRate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.open = true;
      this.title = "添加语言包";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLang(id).then(response => {
        this.form = response.data;
        this.disabled = true;
        this.open = true;
        this.title = "修改语言包";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.validateInput(this.form.lang)) {
            return this.$modal.msgError("语言标识只能包含字母和-");
          }
          if (this.form.id != null) {
            updateLang(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLang(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    validateInput(lang) {
      const pattern = /^[a-zA-Z-]+$/;
      return pattern.test(lang);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除语言包编号为"' + ids + '"的数据项？').then(function() {
        return delLang(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('video/lang/export', {
        ...this.queryParams
      }, `lang_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.form-item-desc {
  font-size: 14px;
  color: #999;
  margin: 0;
}
</style>
